package com.automation.scripts.core.fwd.android;

import com.automation.scripts.common.exceptions.ParamNullException;
import com.automation.scripts.common.util.appium.AndroidAppiumOperator;
import com.automation.scripts.common.util.appium.IAppiumOperator;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.options.UiAutomator2Options;
import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.time.Duration;
import java.util.List;

public class FWDAndroid implements AutoCloseable {
    /**
     * appium服务器地址
     */
    private final String url;

    /**
     * 包名
     */
    private final String pkgName;

    /**
     * apk路径
     */
    private final String appPath;

    /**
     * 驱动
     */
    private AndroidDriver driver;

    /**
     * 操作类
     */
    private IAppiumOperator appiumOperator;

    public FWDAndroid(String url, String pkgName, String appPath) {
        if (StringUtils.isEmpty(url)) {
            throw new ParamNullException("appium url");
        }
        if (StringUtils.isEmpty(pkgName)) {
            throw new ParamNullException("package name");
        }
        if (StringUtils.isEmpty(appPath)) {
            throw new ParamNullException("app path");
        }
        this.url = url;
        this.pkgName = pkgName;
        this.appPath = appPath;
    }

    public void run() throws Exception {
        launchApp();

        // 点击同意或者跳过 都可以进入到主界面
        this.appiumOperator.waitForOne(
                List.of(
                        By.xpath("//android.widget.Button[@content-desc=\"同意\"]"),
                        By.xpath("//android.view.View[starts-with(@content-desc,\"跳过\")]")
                ),
                Duration.ofSeconds(10)
        ).click();

        // 点击弹屏广告
        this.appiumOperator.waitForAll(
                List.of(
                        By.xpath("//android.widget.ImageView"),
                        By.xpath("//android.widget.Button")
                ),
                Duration.ofSeconds(10)
        ).get(1).click();

        this.appiumOperator.waitFor(By.xpath("//android.widget.ImageView[@content-desc=\"我的\"]"),Duration.ofSeconds(5)).click();
        this.appiumOperator.waitFor(By.xpath("//android.view.View[@content-desc=\"立即登录\"]"),Duration.ofSeconds(5)).click();
        WebElement phoneInput = this.appiumOperator.waitFor(By.xpath("//android.widget.EditText"), Duration.ofSeconds(5));
        phoneInput.click();
        phoneInput.clear();
        phoneInput.sendKeys("12345678901");
        //android.widget.FrameLayout[@resource-id="android:id/content"]/android.widget.FrameLayout/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View[3]/android.view.View/android.view.View[3]
        // 00000000-0000-0071-0000-015700000003
        this.appiumOperator.waitFor(By.id("00000000-0000-0071-0000-015700000003"),Duration.ofSeconds(5)).click();
        this.appiumOperator.waitFor(By.xpath("android.view.View[@content-desc=\"验证码登录\"]"),Duration.ofSeconds(5)).click();

        //android.widget.Button[@content-desc="我已知晓(1)"]
        //android.view.View[@content-desc="2025/08/16 12:25:00 开抢"]
    }

    private void launchApp() throws MalformedURLException, URISyntaxException {
        this.driver = new AndroidDriver(
                new URI(this.url).toURL(),
                new UiAutomator2Options()
                        .setPlatformName("Android")
                        .setAutomationName("UiAutomator2")
                        .setDeviceName("Android")
                        .setAppPackage(this.pkgName)
                        .setApp(this.appPath)
//                        .setFullReset(true)
        );
//        if (!driver.isAppInstalled(this.pkgName)) {
//            driver.installApp(this.appPath);
//            System.out.println("安装应用完成");
//        }
//        driver.activateApp(this.pkgName);
//        System.out.println("启动应用");
        this.appiumOperator = new AndroidAppiumOperator(this.driver);
    }

    public void stop() {
        this.appiumOperator.quit();
    }

    @Override
    public void close() throws Exception {
        this.stop();
    }
}
